package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;


    /**
     * 查询检查项
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckItem> checkItems = checkGroupService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItems);
    }



    /**
     * 添加检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        if (checkGroup!=null&&checkitemIds!=null&&checkitemIds.length>0){
            checkGroupService.add(checkGroup,checkitemIds);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }


    /**
     * 检查组分页
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){

        PageInfo<CheckGroup>  checkGroups = checkGroupService.findPage(queryPageBean);

        PageResult<CheckGroup> pageResult = new PageResult<>(checkGroups.getTotal(),checkGroups.getList());

        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
    }


    /**
     * 编辑检查组之回显
     */
    @GetMapping("/findCheckGroupById")
    public Result findCheckGroupById(Integer checkgroupId){
        List<Integer> checkGroupList = checkGroupService.findCheckGroupById(checkgroupId);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS,checkGroupList);
    }


    /**
    * 编辑检查组之修改
    */
    @PostMapping("/edit")
    public Result edit(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){
        checkGroupService.edit(checkGroup,checkitemIds);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }


    /**
     * 删除检查组
     * @param id
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteById(Integer id){
        checkGroupService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

}
